package cn.xspace.wnx.adapter;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;

import com.xspace.android.xafcommon.util.LogUtil;

import java.util.ArrayList;

import cn.xspace.wnx.entity.WNXBean;
import cn.xspace.wnx.view.wnxchat.BaseWnxChatRow;
import cn.xspace.wnx.view.wnxchat.WnxMsgChatRow;

/**
 * Created by Administrator on 2016/11/12.
 */

public class WnxOrgMessageListAdapter extends BaseAdapter {


    private static final String TAG = "WnxOrgMessageListAdapter";
    private ArrayList<WNXBean> dataList = new ArrayList<>();
    LayoutInflater mInflater;
    private static final int HANDLER_MESSAGE_REFRESH_LIST = 0;
    private static final int HANDLER_MESSAGE_SELECT_LAST = 1;
    private static final int HANDLER_MESSAGE_SEEK_TO = 2;
    private ListView mListview;

    private static Context mContext;

    public WnxOrgMessageListAdapter(Context context, ArrayList<WNXBean> dataList, ListView listView) {
        this.dataList = dataList;
        mContext = context;
        mListview = listView;
        mInflater = LayoutInflater.from(mContext);
    }

    @Override
    public int getCount() {
        return dataList.size();
    }

    @Override
    public WNXBean getItem(int i) {
        return dataList.get(i);
    }

    @Override
    public long getItemId(int i) {
        return dataList.get(i).hashCode();
    }


    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        return createView(mContext, getItem(i), i);
    }

    protected BaseWnxChatRow createView(Context context, WNXBean message, int position) {
        BaseWnxChatRow baseWnxChatRow = null;
        try {
            baseWnxChatRow = new WnxMsgChatRow(context, message, position, this);
        } catch (Exception e) {
            e.printStackTrace();
        }
        baseWnxChatRow.setUpView(message, position);
        return baseWnxChatRow;
    }

    /**
     * 刷新列表的方法,通过Handler发送消息去刷新
     */
    public void refresh() {
        LogUtil.d(TAG, "刷新列表,判断是否正在刷新");
        if (handler.hasMessages(HANDLER_MESSAGE_REFRESH_LIST)) {
            return;
        }
        LogUtil.d(TAG, "可以刷新");
        Message msg = handler.obtainMessage(HANDLER_MESSAGE_REFRESH_LIST);
        handler.sendMessage(msg);
    }

    /**
     * 刷新列表并且跳转到最后一个条目
     */
    public void refreshSelectLast() {
        final int TIME_DELAY_REFRESH_SELECT_LAST = 100;
        handler.removeMessages(HANDLER_MESSAGE_REFRESH_LIST);
        handler.removeMessages(HANDLER_MESSAGE_SELECT_LAST);
        handler.sendEmptyMessageDelayed(HANDLER_MESSAGE_REFRESH_LIST, TIME_DELAY_REFRESH_SELECT_LAST);
        handler.sendEmptyMessageDelayed(HANDLER_MESSAGE_SELECT_LAST, TIME_DELAY_REFRESH_SELECT_LAST);
    }

    /**
     * 刷新列表并且跳转到指定的位置
     */
    public void refreshSeekTo(int position) {
        handler.sendMessage(handler.obtainMessage(HANDLER_MESSAGE_REFRESH_LIST));
        Message msg = handler.obtainMessage(HANDLER_MESSAGE_SEEK_TO);
        msg.arg1 = position;
        handler.sendMessage(msg);
    }

    private Handler handler = new Handler() {

        private void refreshList() {
            // you should not call getAllMessages() in UI thread
            // otherwise there is problem when refreshing UI and there is new message arrive
//			java.util.List<EMMessage> var = conversation.getAllMessages();
//			messages = var.toArray(new EMMessage[var.size()]);
//			conversation.markAllMessagesAsRead();
            notifyDataSetChanged();
        }

        @Override
        public void handleMessage(android.os.Message message) {
            switch (message.what) {
                case HANDLER_MESSAGE_REFRESH_LIST: //刷新列表
                    refreshList();
                    break;
                case HANDLER_MESSAGE_SELECT_LAST: //选中最后一个条目
                    if (dataList.size() > 0) {
                        mListview.setSelection(dataList.size() - 1);
                    }
                    break;
                case HANDLER_MESSAGE_SEEK_TO: //跳转到指定的条目
                    int position = message.arg1;
                    mListview.setSelection(position);
                    break;
                default:
                    break;
            }
        }
    };

//    static class ViewHolder {
//        ImageView imgLogo;
//        TextView txtTitle;
//        TextView txtContent;
//        TextView txtKind;
//
//        public void findView(View view) {
////            this.imgLogo = (ImageView) view.findViewById(R.id.img_logo);
////            this.txtTitle = (TextView) view.findViewById(R.id.txt_title);
////            this.txtKind = (TextView) view.findViewById(R.id.txt_kind);
////            this.txtContent = (TextView) view.findViewById(R.id.txt_content);
//        }
//
//        public void setData(JSONObject json) {
//            this.txtTitle.setText(json.optString("title"));
//            this.txtContent.setText(json.optString("summary"));
//            HeadShowUtil.headShow(mContext, json.optString("src"), "1", this.imgLogo, HeadShowUtil.HeadType.UserLogo);
//
////            if (TextUtils.isEmpty(json.optString("src"))) {
////                this.imgLogo.setImageResource(R.drawable.icon_header_default);
////            } else {
////                Picasso.with(mContext)
////                        .load(json.optString("src"))
////                        .placeholder(R.drawable.icon_header_default)
////                        .error(R.drawable.icon_header_default)
////                        .into(this.imgLogo);
////            }
//        }
//    }
}
